/**
 * 事件目标 (数据类)
 */
function sEventTarget() {
    this.__events = [];
    this.__waitTrigger = {};
}

/**
 * 添加事件监听
 * @param {*} type 
 * @param {*} callback 
 * @param {*} opt
 */
sEventTarget.prototype.addEventListener = function(type, callback, opt){
    opt = opt ? opt : {};

    if(opt.once){
        this.__events[type] = [];
    }else if(!this.__events[type]){
        this.__events[type] = [];
    }

    this.__events[type].push(callback);

    //分发事件发生在添加监听之后，触发等待队列事件
    if(this.__waitTrigger[type]){
        var option = this.__waitTrigger[type];

        this.__waitTrigger[type] = null;

        this.dispatchEvent(type, option);
    }
}

/**
 * 触发事件
 * @param {*} type 事件类型
 * @param {*} option 可选项 {param: {}, callback: function(){}, clear: false}
 * @returns 
 */
sEventTarget.prototype.dispatchEvent = function(type, option){
    if(this.__events[type]) {
        for(var i = 0; i < this.__events[type].length; i++){
            this.__events[type][i].call(this, option);
        }

        if(option && option.clear){
            this.removeEventListener(type);
        }
    }else if(option && option.lazyTrigger){
        this.__waitTrigger[type] = option;
    }
}

/**
 * 移除事件监听
 * @param {*} type 
 */
sEventTarget.prototype.removeEventListener = function(type){
    this.__events[type] = null;
}